import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {path: '/login',name: 'login',component: ()=>import('./views/Login'), meta: {isPublic: true}},
  {
    path: '/',
    name: 'main',
    component: ()=>import('./views/Main'),
    redirect: '/categories/create',
    children:[
      {path:'/categories/create',component: ()=>import('./views/CategoryEdit')},
      {path:'/categories/edit/:id',component: ()=>import('./views/CategoryEdit'), props: true},
      {path:'/categories/list',component: ()=>import('./views/CategoryList')},

      {path:'/items/create',component: ()=>import('./views/ItemEdit')},
      {path:'/items/edit/:id',component: ()=>import('./views/ItemEdit'), props: true},
      {path:'/items/list',component: ()=>import('./views/ItemList')},

      {path:'/heroes/create',component: ()=>import('./views/HeroEdit')},
      {path:'/heroes/edit/:id',component: ()=>import('./views/HeroEdit'), props: true},
      {path:'/heroes/list',component: ()=>import('./views/HeroList')},

      {path:'/articles/create',component: ()=>import('./views/ArticleEdit')},
      {path:'/articles/edit/:id',component: ()=>import('./views/ArticleEdit'), props: true},
      {path:'/articles/list',component: ()=>import('./views/ArticleList')},

      {path:'/ads/create',component: ()=>import('./views/AdEdit')},
      {path:'/ads/edit/:id',component: ()=>import('./views/AdEdit'), props: true},
      {path:'/ads/list',component: ()=>import('./views/AdList')},

      {path:'/admin_users/create',component: ()=>import('./views/AdminUserEdit')},
      {path:'/admin_users/edit/:id',component: ()=>import('./views/AdminUserEdit'), props: true},
      {path:'/admin_users/list',component: ()=>import('./views/AdminUserList')}
    ]
  },
  
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to.meta);
  if(!to.meta.isPublic && !localStorage.token) {
    return next('./login')
  }
  next()
})

export default router
